home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 4 / The Arsenal Files 4 (Arsenal Computer).ISO / casm / au116-as.exe / UTIL / DATE.CPP < prev    next >
C/C++ Source or Header  |  1994-03-21  |  3KB  |  163 lines

  1. #include "..\au.hpp"
  2. /******************************************************************/
  3. char *date_to_string(DATE *date)
  4. {
  5.     static char ret_string[9];
  6.     sprintf(ret_string, "%02d-%02d-%02d", date->month, date->day,
  7.             date->year % 100);
  8.     return ret_string;
  9. }
  10. /******************************************************************/
  11. void fdate_to_date(DATE *date, struct ffblk *ffblk)
  12. {
  13.     date->year = (((ffblk->ff_fdate & 0xFE00) >> 9) + 1980) % 100;
  14.     date->month = (ffblk->ff_fdate & 0x01E0) >> 5;
  15.     date->day = ffblk->ff_fdate & 0x001F;
  16.     return;
  17. }
  18.  
  19. /*******************************************************************/
  20. static long date_to_days(DATE *date)
  21. {
  22.     long ret_value;
  23.  
  24.     ret_value=365*(long)date->year    /* without Leap year */
  25.               +(date->year-1)/4     /* +leap years */
  26.               -(date->year-1)/100    /* -leap year once a century */
  27.               +(date->year-1)/400    /* +leap year once every 4th century */
  28.               +date->day            /* +# of days into the year */
  29.               +((date->year % 4)==0 && date->month>2 ? 1:0); /* + 1 on leap year itself */
  30.     switch (date->month)            /* add the month to it */
  31.     {
  32.         case 2:
  33.             ret_value+=31;
  34.             break;
  35.         case 3:
  36.             ret_value+=59;
  37.             break;
  38.         case 4:
  39.             ret_value+=90;
  40.             break;
  41.         case 5:
  42.             ret_value+=120;
  43.             break;
  44.         case 6:
  45.             ret_value+=151;
  46.             break;
  47.         case 7:
  48.             ret_value+=181;
  49.             break;
  50.         case 8:
  51.             ret_value+=212;
  52.             break;
  53.         case 9:
  54.             ret_value+=243;
  55.             break;
  56.         case 10:
  57.             ret_value+=273;
  58.             break;
  59.         case 11:
  60.             ret_value+=304;
  61.             break;
  62.         case 12:
  63.             ret_value+=334;
  64.     }
  65.     return ret_value;
  66. }
  67. /*******************************************************************/
  68. static void days_to_date(DATE *date, long days)
  69. {
  70.     int year, month=1;
  71.     long temp;
  72.  
  73.     temp=days / 146097;     /* In groups of 400 */
  74.     days-=146097*temp;
  75.     year=temp*400;
  76.  
  77.     temp=days / 36524;        /* In groups of 100 */
  78.     days-=36524*temp;
  79.     year+=temp*100;
  80.  
  81.     temp=days / 1461;        /* In groups of 4 */
  82.     days-=1461*temp;
  83.     year+=temp*4;
  84.  
  85.     temp=days / 365;        /* In groups of 1 */
  86.     days-=365*temp;
  87.     year+=temp;
  88.  
  89.     if (year % 4 == 0)
  90.         days++;
  91.  
  92.     if (days>31)          /* move to FEB */
  93.     {
  94.         days-=31;
  95.         month++;
  96.     }
  97.     if (days > ((year % 4 == 0) ? 29:28))          /* Move to March */
  98.     {
  99.         days-=((year % 4 == 0) ? 29:28);
  100.         month++;
  101.     }
  102.     if (days>31)          /* move to April */
  103.     {
  104.         days-=31;
  105.         month++;
  106.     }
  107.     if (days>30)          /* move to May */
  108.     {
  109.         days-=30;
  110.         month++;
  111.     }
  112.     if (days>31)          /* move to June */
  113.     {
  114.         days-=31;
  115.         month++;
  116.     }
  117.     if (days>30)          /* move to July */
  118.     {
  119.         days-=30;
  120.         month++;
  121.     }
  122.     if (days>31)          /* move to August */
  123.     {
  124.         days-=31;
  125.         month++;
  126.     }
  127.     if (days>31)          /* move to September */
  128.     {
  129.         days-=31;
  130.         month++;
  131.     }
  132.     if (days>30)          /* move to October */
  133.     {
  134.         days-=30;
  135.         month++;
  136.     }
  137.     if (days>31)          /* move to November */
  138.     {
  139.         days-=31;
  140.         month++;
  141.     }
  142.     if (days>30)          /* move to December */
  143.     {
  144.         days-=30;
  145.         month++;
  146.     }
  147.     date->year    = year;
  148.     date->month = month;
  149.     date->day    = days;
  150.     return;
  151. }
  152.  
  153. /******************************************************************/
  154. void date_add(DATE *s, DATE *t, long days)
  155. {
  156.     days_to_date(t, date_to_days(s) + days);
  157. }
  158. /******************************************************************/
  159. long days_diff(DATE *s, DATE *e)
  160. {
  161.     return date_to_days(e) - date_to_days(s);
  162. }
  163.